package me.learningandroid.ch3_1.ConfigFileIo;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.gson.Gson;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.stream.Collectors;

public class SharedPreferencesActivity extends AppCompatActivity {
    private static final String FILENAME="config2";
    private static final String SHAREDPREF_TEXT="TEXT";
    private static final String SHAREDPREF_SELECTEDRADIOID="SELECTEDRADIOID";
    private static final String SHAREDPREF_CHECKED="CHECKED";
    public static void startActivity(Context context) {
        Intent intent = new Intent(context, SharedPreferencesActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shared_preferences);

        EditText editText = findViewById(R.id.editText);
        RadioGroup radioGroup = findViewById(R.id.radioGroup);
        CheckBox checkBox = findViewById(R.id.checkBox);

        Button applyBtn = findViewById(R.id.applyBtn);
        Button saveBtn = findViewById(R.id.saveBtn);

        applyBtn.setOnClickListener(v -> {
            SharedPreferences sharedPreferences = getSharedPreferences(FILENAME, MODE_PRIVATE);
            if (sharedPreferences.contains(SHAREDPREF_TEXT)) {
                editText.setText(sharedPreferences.getString(SHAREDPREF_TEXT, ""));
            }
            if (sharedPreferences.contains(SHAREDPREF_SELECTEDRADIOID)) {
                radioGroup.check(sharedPreferences.getInt(SHAREDPREF_SELECTEDRADIOID, 0));
            }
            if (sharedPreferences.contains(SHAREDPREF_CHECKED)) {
                checkBox.setChecked(sharedPreferences.getBoolean(SHAREDPREF_CHECKED,false));
            }
        });

        saveBtn.setOnClickListener(v -> {
            String text = editText.getText().toString();
            int selectedRadioId = radioGroup.getCheckedRadioButtonId();
            boolean checked = checkBox.isChecked();
            SharedPreferences sharedPreferences = getSharedPreferences(FILENAME, MODE_PRIVATE);
            sharedPreferences.edit()
                    .putString(SHAREDPREF_TEXT, text)
                    .putInt(SHAREDPREF_SELECTEDRADIOID, selectedRadioId)
                    .putBoolean(SHAREDPREF_CHECKED, checked)
                    .apply();
        });

        FloatingActionButton nextBtn = findViewById(R.id.floatingActionButton);
        nextBtn.setOnClickListener(v -> {
            SharedPreferencesActivity.startActivity(this);
        });
    }

    private static class SaveObj {

        public String text;
        public int selectedRadioId;
        public boolean checked;

        public SaveObj(String text, int selectedRadioId, boolean checked) {
            this.text = text;
            this.selectedRadioId = selectedRadioId;
            this.checked = checked;
        }
    }
}